#import <UIKit/UIKit.h>
#import "AFNetworking.h"

extern NSString * const     FSServerRequestKeepCacheKey;		//保留缓存关键字

#define ERR_NETWORK			1000        //网络故障代码
#define ERR_NODATA			990         //没有数据了

//本地化数据库表名
#define DB_USER				@"fs_user"
#define DB_USER_FRIEND		@"fs_user_friend"

#define DB_MEMBER			@"fs_member"

#define DB_SPORT			@"fs_sport"
#define DB_SPORT_DATA		@"fs_sport_data"
#define DB_RECORD			@"fs_record"

#define DB_GROUP			@"fs_group"
#define DB_GROUP_SIGN		@"fs_group_sign"
#define DB_GROUP_SCORE		@"fs_group_score"
#define DB_GROUP_MEMBER		@"fs_group_member"

#define DB_DEVICE			@"fs_device"
#define DB_REGION			@"fs_region"

CG_INLINE NSString * NSStringFromDictionary(NSDictionary *dict)
{
	if (!dict.count) return @"";
	
	NSMutableArray *ary = [NSMutableArray array];
	for (NSString *key in dict.allKeys)
	{
		id obj = dict[key];
		
		if ([obj isKindOfClass:[NSString class]])
		{
			NSString *str = [obj stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
			[ary addObject:[NSString stringWithFormat:@"\"%@\":\"%@\"", key, str]];
		}
		else if ([obj isKindOfClass:[NSNumber class]])
			[ary addObject:[NSString stringWithFormat:@"\"%@\":%@", key, obj]];
		
		else if ([obj isKindOfClass:[NSDictionary class]])
			[ary addObject:[NSString stringWithFormat:@"\"%@\":%@", key, NSStringFromDictionary(obj)]];
	}
	
	return [NSString stringWithFormat:@"{%@}", [ary componentsJoinedByString:@","]];
}

//
typedef NS_ENUM(NSUInteger, FSProgressState) {
	FSProgressStateStart			,
	FSProgressStateProcess		,
	FSProgressStateSuccess		,
	FSProgressStateFailure		,
};

typedef void(^FSImageResult)		(UIImage *image, NSError *error);
typedef void(^FSServerResult)		(id responseObject, NSError *error);
typedef void(^FSProgressHandler)	(FSProgressState state, CGFloat progress);
typedef void(^FSLoadImageResult)	(UIImage *image, BOOL isDefault);

typedef NS_ENUM(NSUInteger, FSImageType) {
	FSImageTypeURL				,		//按URL路径获取图像（不会在本地保存）
	
	FSImageTypeUser				,		//用户头像			240*240
	FSImageTypeUserCover		,		//用户封面			600*600
	
	FSImageTypeGroup			,		//群组图标			240*240
	FSImageTypeGroupCover		,		//群组封面			600*600
	
	FSImageTypeDevice			,		//获取设备外观图		600*600(不可保存)
};

typedef NS_ENUM(int, FSRankType) {
	FSRankTypeDay			,
	FSRankTypeWeek			,
	FSRankTypeMonth			,
	FSRankTypeYear			,
	FSRankTypeAll			,
};

//============================================================
@class MRDatabase, FSUser;
typedef void(^FSRankingResult) (NSArray <FSUser *> *users, NSError *error);

@interface FSServer : NSObject

+ (MRDatabase *)database;			//本地数据库

+ (FSServer *)server;

+ (AFHTTPSessionManager *)manager;

+ (void)registerApp:(NSString *)appKey;

+ (id)requestWithTarget:(NSString *)target param:(NSDictionary *)params;
+ (id)requestWithTarget:(NSString *)target param:(NSDictionary *)params result:(FSServerResult)result;

//获取服务器图像，非FSImageTypeURL时，url传入对应图像文件名
+ (id)loadImageWithType:(FSImageType)type url:(NSString *)url result:(FSImageResult)result;

//保存图像时传入的target为对应类型相关ID，如用户ID、群组ID
+ (id)saveImageWithType:(FSImageType)type target:(uint)target image:(UIImage *)image progress:(FSProgressHandler)handler result:(FSServerResult)result;

//其它
+ (id)rankingWithType:(FSRankType)type range:(NSRange)range param:(NSDictionary *)param result:(FSRankingResult)result;
+ (id)feedbackWithParam:(NSDictionary *)param result:(FSServerResult)result;
+ (id)deviceDetail:(uint)device result:(FSServerResult)result;

//缓存
+ (float)sizeOfCache;
+ (void)cleanCache;

@end
